<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_ci_type.class.php');


class TCMS_CI_Type_DBC extends TCMS_CI_Type implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select name, ".
						"hid, ".
						"page_edit_path, ".
						"page_manage_path, ".
						"page_select_path, ".
						"page_delete_path, ".
						"page_preview_path, ".
						"class_path, ".
						"class_name, ".
						"capture_prefix, ".
						"db_table, ".
						"allow_images, ".
						"allow_attachments ".
					"from tcms_ci_types ".
					"where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_name(stripslashes($rez_obj->NAME));
			$this->set_hid(stripslashes($rez_obj->HID));
			$this->set_page_edit_path(stripslashes($rez_obj->PAGE_EDIT_PATH));
			$this->set_page_manage_path(stripslashes($rez_obj->PAGE_MANAGE_PATH));
			$this->set_page_select_path(stripslashes($rez_obj->PAGE_SELECT_PATH));
			$this->set_page_delete_path(stripslashes($rez_obj->PAGE_DELETE_PATH));
			$this->set_page_preview_path(stripslashes($rez_obj->PAGE_PREVIEW_PATH));
			$this->set_class_path(stripslashes($rez_obj->CLASS_PATH));
			$this->set_class_name(stripslashes($rez_obj->CLASS_NAME));
			$this->set_capture_prefix(stripslashes($rez_obj->CAPTURE_PREFIX));
			$this->set_db_table(stripslashes($rez_obj->DB_TABLE));
			$this->set_allow_images($rez_obj->ALLOW_IMAGES);
			$this->set_allow_attachments($rez_obj->ALLOW_ATTACHMENTS);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_ci_types_seq');
		$name = addslashes($this->get_name());
		$hid = addslashes($this->get_hid());
		$page_edit_path = addslashes($this->get_page_edit_path());
		$page_manage_path = addslashes($this->get_page_manage_path());
		$page_select_path = addslashes($this->get_page_select_path());
		$page_delete_path = addslashes($this->get_page_delete_path());
		$page_preview_path = addslashes($this->get_page_preview_path());
		$class_path = addslashes($this->get_class_path());
		$class_name = addslashes($this->get_class_name());
		$capture_prefix = addslashes($this->get_capture_prefix());
		$db_table = addslashes($this->get_db_table());
		$allow_images = $this->get_allow_images() ? 1 : 0;
		$allow_attachments = $this->get_allow_attachments() ? 1 : 0;

		if (!$this->is_unique_hid_insert($dbc, $hid)) {
			throw new Tangra_Module_Exception('Not unique hid: '.$hid);
		}

		if ($id) {
			$sql = "insert into tcms_ci_types (id, ".
									      "name, ".
									      "hid, ".
									      "page_edit_path, ".
									      "page_manage_path, ".
									      "page_select_path, ".
									      "page_delete_path, ".
									      "page_preview_path, ".
									      "class_path, ".
									      "class_name, ".
									      "capture_prefix, ".
									      "db_table, ".
									      "allow_images, ".
									      "allow_attachments ".
									      ") ".
								"values ".
									      "($id,".
									      "'$name', ".
									      "'$hid', ".
									      "'$page_edit_path', ".
									      "'$page_manage_path', ".
									      "'$page_select_path', ".
									      "'$page_delete_path', ".
									      "'$page_preview_path', ".
									      "'$class_path', ".
									      "'$class_name', ".
									      "'$capture_prefix', ".
									      "'$db_table', ".
									      "$allow_images, ".
									      "$allow_attachments ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_ci_types_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$name = addslashes($this->get_name());
		$hid = addslashes($this->get_hid());
		$page_edit_path = addslashes($this->get_page_edit_path());
		$page_manage_path = addslashes($this->get_page_manage_path());
		$page_select_path = addslashes($this->get_page_select_path());
		$page_delete_path = addslashes($this->get_page_delete_path());
		$page_preview_path = addslashes($this->get_page_preview_path());
		$class_path = addslashes($this->get_class_path());
		$class_name = addslashes($this->get_class_name());
		$capture_prefix = addslashes($this->get_capture_prefix());
		if (!$this->is_unique_hid_update($dbc, $hid, $id)) {
			throw new Tangra_Module_Exception('Not unique hid: '.$hid);
		}
		$db_table = addslashes($this->get_db_table());
		$allow_images = $this->get_allow_images() ? 1 : 0;
		$allow_attachments = $this->get_allow_attachments() ? 1 : 0;


		$sql = "update tcms_ci_types set ".
						"name = '$name', ".
						"hid = '$hid', ".
						"page_edit_path = '$page_edit_path', ".
						"page_manage_path = '$page_manage_path', ".
						"page_select_path = '$page_select_path', ".
						"page_delete_path = '$page_delete_path', ".
						"page_preview_path = '$page_preview_path', ".
						"class_path = '$class_path', ".
						"class_name = '$class_name', ".
						"capture_prefix = '$capture_prefix', ".
						"db_table = '$db_table', ".
						"allow_images = $allow_images, ".
						"allow_attachments = $allow_attachments ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	private function is_unique_hid_insert(DB_Connection $dbc, $hid) {
		$sql = "select id from tcms_ci_types where hid = '$hid'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	private function is_unique_hid_update(DB_Connection $dbc, $hid, $id) {
		$sql = "select id from tcms_ci_types where hid = '$hid' and id <> $id";

		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_ci_types where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid() {
		$sql = "select id, name, hid, capture_prefix from tcms_ci_types";

		return $sql;
	}


	public static function get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from tcms_ci_types";

		return $sql;
	}


	public static function load_types_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from tcms_ci_types order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}


		return $ret;
	}


	public function load_by_hid(DB_Connection $dbc, $hid) {
		$hid = addslashes($hid);
		$ret = false;

		$sql = "select id from tcms_ci_types where hid = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function is_unique_hid(DB_Connection $dbc, $ci_id, $hid) {
		tangra_if_not_int_throw_e($ci_id);
		$hid = addslashes($hid);

		$sql = "select id from tcms_ci_types where id <> $ci_id and hid = '$hid'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public static function load_ci_types_for_template(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);
		$ret = array();

		$sql = "select ci.id, ".
						"ci.ci_name, ".
						"t.name, ".
						"ci.required, ".
						"ci.export_name, ".
						"t.page_edit_path, ".
						"t.page_manage_path, ".
						"t.page_select_path, ".
						"t.page_delete_path, ".
						"t.page_preview_path, ".
						"t.capture_prefix ".
					"from tcms_vpt_cis ci, ".
							"tcms_ci_types t ".
					"where template = $template_id and ".
							"ci.ci_type = t.id ".
					"order by required desc";
		$rez = $dbc->execute($sql);

		$c = 0;
		while ($rez_obj = $rez->fetch_object()) {
			$ret[$c]['id'] = $rez_obj->ID;
			$ret[$c]['ci_name'] = $rez_obj->CI_NAME;
			$ret[$c]['type'] = $rez_obj->NAME;
			$ret[$c]['required'] = $rez_obj->REQUIRED;
			$ret[$c]['export_name'] = $rez_obj->EXPORT_NAME;
			$ret[$c]['page_edit_path'] = $rez_obj->PAGE_EDIT_PATH;
			$ret[$c]['page_manage_path'] = $rez_obj->PAGE_MANAGE_PATH;
			$ret[$c]['page_select_path'] = $rez_obj->PAGE_SELECT_PATH;
			$ret[$c]['page_delete_path'] = $rez_obj->PAGE_DELETE_PATH;
			$ret[$c]['page_preview_path'] = $rez_obj->PAGE_PREVIEW_PATH;
			$ret[$c]['capture_prefix'] = $rez_obj->CAPTURE_PREFIX;
			$c++;
		}

		return $ret;
	}


	public static function load_cit_that_use_attr_type(DB_Connection $dbc, $attr_type_id) {
		tangra_if_not_int_throw_e($attr_type_id);
		$sql = "select distinct cit.id as id ".
					"from tcms_ci_types cit, ".
							"tcms_ci_type_attrs cita ".
						"where cita.attr_type =  $attr_type_id and ".
								"cita.ci_type = cit.id";

		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_CI_Type_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[] = clone $tmp;
		}

		return $ret;
	}


	public static function load_all_types(DB_Connection $dbc) {
		$sql = "select id from tcms_ci_types order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();

		while($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_CI_Type_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);

			$ret[] = clone $tmp;
		}

		return $ret;
	}
}